package org.vincen.helloworld.jaxb;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.NotFoundException;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.FieldInfo;
import javassist.bytecode.annotation.Annotation;

import org.junit.Test;
import org.vincen.helloworld.jaxb.JAXBHelper.GenerateJAXBPOJO;

public class PracticeJAXB1 {

	@Test
	public void test1() throws NotFoundException, ClassNotFoundException, IOException, CannotCompileException{
		String clsName = POJO.class.getName();
//		System.out.println(clsName + "");
		
//		CtClass cc = ClassPool.getDefault().get("org.vincen.helloworld.jaxb.POJO");
		CtClass cc = ClassPool.getDefault().get(clsName);
		CtField[] cf = cc.getDeclaredFields();
		FieldInfo info = null;
		ClassFile classFile = cc.getClassFile();
		ConstPool constPool = classFile.getConstPool();
		AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		Annotation annotation = new Annotation("javax.xml.bind.annotation.XmlElement", constPool);
		//给注解，设置值。
//		annotation.addMemberValue("value", new IntegerMemberValue(constPool, 0));
		attr.addAnnotation(annotation);
		for (CtField f : cf){
			info = f.getFieldInfo();
//			String annoName = info.getName();
//			System.out.println(annoName);
			info.addAttribute(attr);
			
//			System.out.println("print the annotations:");
//			Object[] oo = f.getAnnotations();
//			for (Object o : oo){
//				System.out.println(o);
//			}
		}
		Class<?> clazz = cc.toClass();
		POJO pojo = new POJO();
		pojo.setName("vincen");
		pojo.setAge(26);
		JAXBHelper helper = new JAXBHelper(clazz);
		String s = helper.buildMarshalString(pojo);
		System.out.println(s);
	}
	
//	@Test
//	public void test2(){
//		String clsName = "org.vincen.helloworld.jaxb.POJO";
//		Class<?> c = JAXBHelper.buildOriginalClass(clsName);
//		Component comp = new Component();
//		POJO dest = comp.read();
//		JAXBHelper h = new JAXBHelper(c);
//		String s = h.buildMarshalString(dest);
//		System.out.println(s);
//		POJO afj = new POJO();
//		afj.setAge(33);
//		afj.setName("gao");
//		System.out.println(afj.getAge());
//		System.out.println(afj.getName());
//	}
	
	@Test
	public void test3() {
		POJO afj = new Component().read();
		JAXBHelper h = new JAXBHelper(POJO.class);
		System.out.println(h.buildMarshalString(afj));
	}
	
	@Test
	public void test4(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("age", "aage");
		map.put("name", "aname");
//		JAXBHelper h = new JAXBHelper(GenerateJAXBPOJO.genFullPOJO("org.vincen.helloworld.jaxb.POJO"));
//		JAXBHelper h = new JAXBHelper(GenerateJAXBPOJO.genChoosedPOJO("org.vincen.helloworld.jaxb.POJO", false, "age"));
//		JAXBHelper h = new JAXBHelper(GenerateJAXBPOJO.genChoosedPOJO("org.vincen.helloworld.jaxb.POJO", true, "name"));
		JAXBHelper h = new JAXBHelper(GenerateJAXBPOJO.genChangedPOJO("org.vincen.helloworld.jaxb.POJO", map));
//		JAXBHelper h = new JAXBHelper(GenerateJAXBPOJO.genChoosedPOJO2(POJO.class));
//		JAXBHelper h = new JAXBHelper(GenerateJAXBPOJO.genFace(POJO.class));
//		JAXBHelper h = new JAXBHelper(new GenerateJAXBPOJO(POJO.class).genChoosedPOJO());
		POJO afj = new Component().read();
		System.out.println(h.buildMarshalString(afj));
	}
}